package com.approval.approvalapp.message.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.WtrBean;
import com.approval.approvalapp.index.cygn.wzly.WzlyQdAdapter;

import java.util.List;

/**
 * Created by louyulin on 2019/5/31.
 */

public class WtrAdapter extends RecyclerView.Adapter<WtrAdapter.Holder> {

    List<WtrBean> list;
    String uid;
    ClickListener listener;

    public void setList(List<WtrBean> list, String uid, ClickListener listener) {
        this.list = list;
        this.uid = uid;
        this.listener = listener;
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_wtr, parent, false);
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        holder.textView.setText(list.get(position).getWTNAME());
        if (!TextUtils.isEmpty(uid)) {
            if (list.get(position).getWTUSERID().equals(uid)) {
                holder.iv.setBackgroundResource(R.drawable.case_on);
            } else {
                holder.iv.setBackgroundResource(R.drawable.case_off);
            }
        } else {
            holder.iv.setBackgroundResource(R.drawable.case_off);
        }
        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.click(list.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        } else {
            return list.size();
        }
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView textView;
        ImageView iv;
        RelativeLayout root;

        public Holder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv1);
            iv = itemView.findViewById(R.id.iv);
            root = itemView.findViewById(R.id.root);
        }
    }

    public interface ClickListener {
        public void click(WtrBean wtrBean);
    }
}
